package com.jams.library.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jams.library.R;


public class CommonMenuItem extends RelativeLayout {
    private ImageView leftImage;
    private TextView leftText;
    private TextView rightText;
    private TextView rightRedDotView;
    private ImageView rightImage;
    private View bottomDivider;

    public CommonMenuItem(Context context) {
        super(context, null);
    }

    public CommonMenuItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.common_menu_item, this, true);
        leftImage = (ImageView) findViewById(R.id.iv_common_menu_left);
        leftText = (TextView) findViewById(R.id.tv_common_menu_left);
        rightText = (TextView) findViewById(R.id.tv_common_menu_right);
        rightRedDotView = (TextView) findViewById(R.id.tv_common_menu_dot_right);
        rightImage = (ImageView) findViewById(R.id.iv_common_menu_right);
        bottomDivider = findViewById(R.id.view_common_divider);
        TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.CommonMenuItem);
        if (attributes != null) {
            //左边image
            if (attributes.hasValue(R.styleable.CommonMenuItem_left_image)) {
                leftImage.setVisibility(VISIBLE);
                int leftImageUrl = attributes.getResourceId(R.styleable.CommonMenuItem_left_image, -1);
                leftImage.setImageResource(leftImageUrl);
            } else {
                leftImage.setVisibility(GONE);
            }

            if (attributes.hasValue(R.styleable.CommonMenuItem_left_hint)) {
                String hint = attributes.getString(R.styleable.CommonMenuItem_left_hint);
                leftText.setHint(hint);
            }
            if (attributes.hasValue(R.styleable.CommonMenuItem_left_hint_color)) {
                leftText.setHintTextColor(attributes.getColor(R.styleable.CommonMenuItem_left_hint_color, -1));
            }
            if (attributes.hasValue(R.styleable.CommonMenuItem_left_text_size)) {
                leftText.setTextSize(TypedValue.COMPLEX_UNIT_PX, attributes.getDimension(R.styleable.CommonMenuItem_left_text_size, 15));
            }
            //左边文字
            if (attributes.hasValue(R.styleable.CommonMenuItem_left_text)) {
                String leftTv = attributes.getString(R.styleable.CommonMenuItem_left_text);
                leftText.setText(leftTv);
            }
            if (attributes.hasValue(R.styleable.CommonMenuItem_left_text_color)) {
                leftText.setTextColor(attributes.getColor(R.styleable.CommonMenuItem_left_text_color, -1));
            }
            //右边文字
            if (attributes.hasValue(R.styleable.CommonMenuItem_right_text)) {
                rightText.setVisibility(VISIBLE);
                String rightTv = attributes.getString(R.styleable.CommonMenuItem_right_text);
                rightText.setText(rightTv);
                if (attributes.hasValue(R.styleable.CommonMenuItem_right_text_color)) {
                    rightText.setTextColor(attributes.getColor(R.styleable.CommonMenuItem_right_text_color, -1));
                }
                if (attributes.hasValue(R.styleable.CommonMenuItem_right_text_size)) {
                    rightText.setTextSize(TypedValue.COMPLEX_UNIT_PX, attributes.getDimension(R.styleable.CommonMenuItem_right_text_size, 15));
                }
            } else {
                rightText.setVisibility(GONE);
            }

            //右边箭头
            if (getStatus(attributes, R.styleable.CommonMenuItem_right_image_show)) {
                rightImage.setVisibility(VISIBLE);
            } else {
                rightImage.setVisibility(INVISIBLE);
            }
            //底部分割线
            if (getStatus(attributes, R.styleable.CommonMenuItem_bottom_divider)) {
                bottomDivider.setVisibility(VISIBLE);
            } else {
                bottomDivider.setVisibility(GONE);
            }

        }
        if (attributes != null) {
            attributes.recycle();
        }
    }

    public void setRightInfoTV(String text) {
        rightText.setVisibility(VISIBLE);
        rightText.setText(text);
    }

    public void setLeftInfoTV(String text) {
        leftText.setVisibility(VISIBLE);
        leftText.setText(text);
    }

    //设置红点里面的值
    public void setPointNum(int num) {
        rightRedDotView.setVisibility(num > 0 ? VISIBLE : GONE);
        if (num > 99) {
            rightRedDotView.setText("99+");
        } else {
            rightRedDotView.setText(num + "");
        }
    }

    public String getLeftInfoTv() {
        if (leftText.getText().toString().isEmpty()) {
            return null;
        }
        return leftText.getText().toString();
    }

    public void showRedDot(Boolean bShow) {
        int visibleValue = bShow ? VISIBLE : INVISIBLE;
        rightRedDotView.setVisibility(visibleValue);
    }

    private boolean getStatus(TypedArray attributes, int resId) {
        return attributes.getBoolean(resId, false);
    }
}
